package cse.om.common;

import cse.om.data.Comment;
import cse.om.data.DatabaseManager;
import cse.om.data.Profile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Utils {

	private static List<Comment> _listComments;
	private static Map<Profile, List<Comment>> _mapComments;

	public static List<Comment> getCommentsFromFile(String filePath, boolean forceUpdate) {
		if (_listComments != null && !forceUpdate) {
			return _listComments;
		}
		try {
			_listComments = new ArrayList<>();
			String[] comments = UTF8File.getAllLines(filePath);
			for (String c : comments) {
				String[] data = c.split("\t");
				Comment comment = new Comment();
				comment.setId(data[0]);
				comment.setContent(data[1]);
				comment.setSource(data[2]);
				comment.setFromUserId(Long.parseLong(data[3]));
				comment.setToUserId(Long.parseLong(data[4]));
				comment.setOwnerUserId(Long.parseLong(data[5]));
				comment.setTime(data[6]);
				_listComments.add(comment);
			}
			return _listComments;
		} catch (IOException ex) {
			Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
			return null;
		}
	}

	public static List<Comment> getCommentsFromDatabase(boolean forceUpdate) {
		if (_listComments == null || forceUpdate) {
			_listComments = DatabaseManager.getAllComments();
		}
		return _listComments;
	}

	public static Map<Profile, List<Comment>> getMapProfileWithComments(boolean forceUpdate) {
		if (_mapComments != null && !forceUpdate) {
			return _mapComments;
		}
		getCommentsFromDatabase(false);
		_mapComments = new HashMap<>();
		for (Comment cm : _listComments) {
			Profile owner = DatabaseManager.getProfileById(cm.getOwnerUserId());
			if (!_mapComments.containsKey(owner)) {
				_mapComments.put(owner, new ArrayList<Comment>());
			}
			_mapComments.get(owner).add(cm);
		}
		return _mapComments;
	}

	public static Map<Profile, List<Comment>> getMapProfileWithComments(
		boolean forceUpdate, int minOfComment) {
		getMapProfileWithComments(forceUpdate);
		Set<Profile> profiles = _mapComments.keySet();
		Map<Profile, List<Comment>> result = new HashMap<>();
		for (Profile p : profiles) {
			if (_mapComments.get(p).size() >= minOfComment) {
				result.put(p, _mapComments.get(p));
			}
		}
		return result;
	}

	public static Map<Long, Object[]> getMapProfileIdWithCommentCount() {
		return DatabaseManager.getCommentCount();
	}

	public static List<Comment> getCommentsFromDatabase(Long profileId, Date timeFrom, Date timeTo) {
		return DatabaseManager.getCommentByOwnerId(profileId, timeFrom, timeTo);
	}
	
	public static List<String> getCommentContent(List<Comment> commentList) {
		List<String> result = new ArrayList<>();
		for (Comment cm : commentList) {
			result.add(cm.getContent());
		}
		return result;
	}
}
